
import os, bpy, bpy.ops, math, mathutils, ossl.util as util, bmesh
from bpy.types import Operator
from mathutils import Vector
from math import pi

class Spring(object):
    __slots__ = (
        "wire_thickness",
        "wire_section_segments",
        "spring_radius",
        "revolutions",
        "revolution_segments",
        "revolution_height_difference",
        "spiral",
        )

    def __init__(self,
        wire_thickness,
        wire_section_segments,
        spring_radius,
        revolutions,
        revolution_segments,
        revolution_height_difference,
        ):

        self.wire_thickness = wire_thickness
        self.wire_section_segments = wire_section_segments
        self.spring_radius = spring_radius
        self.revolutions = revolutions
        self.revolution_segments = revolution_segments
        self.revolution_height_difference = revolution_height_difference
        self.spiral = None

    def new(self, newScene=None):
        util.setMode("OBJECT")

        if newScene is not None:
            scene = bpy.data.scenes.new(name=newScene)
            bpy.context.window.scene = scene
        bpy.ops.object.select_all(action='DESELECT')

        self.spiral = bpy.data.objects.new("Spiral", bpy.data.meshes.new('SpiralMesh'))
        self.spiral.location = bpy.context.scene.cursor.location
        bpy.context.scene.collection.objects.link(self.spiral)

        bpy.ops.object.select_all(action='DESELECT')
        bpy.context.view_layer.objects.active = self.spiral
        util.setMode("EDIT")

        bm = bmesh.from_edit_mesh(self.spiral.data)
        bm.verts.ensure_lookup_table()
        bm.edges.ensure_lookup_table()
        bm.faces.ensure_lookup_table()

        verts = [ (0,0,0), (0,1,0), (1,1,0), (1,0,0) ]
        for v in verts: bm.verts.new(v)
        bm.verts.index_update()
        bm.verts.ensure_lookup_table()

        bm.faces.new([bm.verts[i] for i in range(0, 4)])
        bm.faces.index_update()
        bm.faces.ensure_lookup_table()
        
        #for i in range(0, self.numVerts): bm.verts[i].normal = self.norms[i]
        
        bpy.ops.mesh.select_mode(type='FACE')
        bpy.ops.mesh.select_all(action="SELECT")
        bpy.ops.mesh.faces_shade_smooth()

        bm.loops.layers.uv.verify()

        bmesh.update_edit_mesh(self.spiral.data)
        bm.free()

        util.setMode("OBJECT")

        return self.spiral

############## MenuFolder ###############

import bpy, ossl.menu as menu

class OSSL_MT_Spring(menu.MenuFolder):
    bl_label = "OSSL Menu Spring"
    bl_idname = "OSSL_MT_Spring"

def OSSL_menu_Spring(self, comtext):
    self.layout.menu("OSSL_MT_Spring", text="Spring", icon="MOD_SCREW")

import ossl.util as util

def register():
    print("registering: OSSL Type Pool.Spring")
    bpy.utils.register_class(OSSL_MT_Spring)
    bpy.types.OSSL_MT_Pool.append(OSSL_menu_Spring)
    util.imports("ossl._types.Pool.Spring", "generate_default")
    print("registered: OSSL Type Pool.Spring")

def unregister():
    print("unregistering: OSSL Type Pool.Spring")
    util.release("ossl._types.Pool.Spring", "generate_default")
    bpy.types.OSSL_MT_Pool.remove(OSSL_menu_Spring)
    bpy.utils.unregister_class(OSSL_MT_Spring)
    print("unregistered: OSSL Type Pool.Spring")

if __name__ == "__main__":
    util.reset_addon()
